local daojie = fk.CreateSkill {
  name = "rmt__daojie",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["rmt__daojie"] = "蹈节",
  -- 若此牌仍在处理区
  [":rmt__daojie"] = "锁定技，当你每回合首次使用非伤害类普通锦囊牌后，汤圆须选择一项：1.失去1点体力；2.失去一个锁定技。然后令一名扶摇势力角色获得此牌。",
  ["#rmt__daojie-choice"] = "蹈节：选择失去一个锁定技，或失去1点体力",
  ["#rmt__daojie-choose"] = "蹈节：令一名符合角色获得此%arg",
  ["daojie_cancel"] = "取消",
  [":daojie_cancel"] = "失去1点体力",
}

--- 判断某事件是否为指定父事件的子事件
local function isParent(son, pat)
  local temp = son
  repeat
    if temp.id == pat.id then return true end
    temp = temp.parent
  until temp == nil
  return false
end

--- 使用的牌是否为回复牌
---@param event GameEvent @ 使用事件
local function isRecoverCard(event)
  local use = event.data
  local card = use.card
  if table.contains({"god_salvation", "peach"}, card.trueName) then return true end
  -- 搜寻使用过程中此牌是否造成回复体力，可用于搜索濒死喝酒、受伤喝散
  return #event:searchEvents(GameEvent.Recover, 1, function (e)
    local recover = e.data
    return recover.card == card and isParent(e, event)
  end) > 0
end

daojie:addEffect(fk.CardUseFinished, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(daojie.name) then return false end
    -- 我到底是不是汤圆啊？
    --if player.general ~= "rmt__tangyuan" and player.deputyGeneral ~= "rmt__tangyuan" then return false end
    local room = player.room
    local recoverd_players = {player}
    if target ~= player and player:hasSkill("rmt__tonggui") then
      room.logic:getEventsOfScope(GameEvent.Recover, 1, function (e)
        local recover = e.data
        table.insertIfNeed(recoverd_players, recover.who)
        return false
      end, Player.HistoryGame)
    end
    if not table.contains(recoverd_players, target) then return false end
    local use_event = room.logic:getCurrentEvent()
    if use_event:findParent(GameEvent.Turn) == nil then return false end
    local cardlist = data.card:isVirtual() and data.card.subcards or {data.card.id}
    if #cardlist == 0 or not table.every(cardlist, function (id)
      return room:getCardArea(id) == Card.Processing
    end) then return false end
    local recover = player:hasSkill("rmt__guofu")
    local function fitCard(e)
      local c = e.data.card
      return (c:isCommonTrick() and not c.is_damage_card) or (recover and isRecoverCard(e))
    end
    if not fitCard(use_event) then return false end
    local first_id = -1
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
      local use = e.data
      if use.from == target and fitCard(e) then
        first_id = e.id
        return true
      end
      return false
    end, Player.HistoryTurn)
    return use_event.id == first_id
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skills = {}
    for _, skill in ipairs(player.player_skills) do
      if skill:hasTag(Skill.Compulsory, false) and skill:isPlayerSkill(player) then
        table.insert(skills, skill.name)
      end
    end
    table.insert(skills, "daojie_cancel")
    local choice = room:askToChoice(player, {
      choices = skills,
      skill_name = daojie.name,
      prompt = "#tea__yihuo-invoke",
      detailed = true,
    })
    if choice == "daojie_cancel" then
      room:loseHp(player, 1, self.name)
    else
      room:handleAddLoseSkills(player, "-"..choice, nil, true, false)
    end
    if not player.dead and table.every(Card:getIdList(data.card), function (id)
      return room:getCardArea(id) == Card.Processing
    end) then
      local recoverd_players = {}
      room.logic:getEventsOfScope(GameEvent.Recover, 1, function (e)
        local recover = e.data
        table.insertIfNeed(recoverd_players, recover.who)
        return false
      end, Player.HistoryGame)
      local targets = table.filter(room.alive_players, function (p)
        return p.kingdom == "fuyao" or p.kingdom == "fuyaoz"
        or (player:hasSkill("rmt__fuyaoo") and not table.contains(recoverd_players, p)) end)
      if #targets == 0 then return end
      if #targets > 1 then
        targets = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#rmt__daojie-choose:::"..data.card:toLogString(),
        skill_name = daojie.name,
        cancelable = false,
        })
      end
      room:obtainCard(targets[1], data.card, true, fk.ReasonPrey, player, self.name)
    end
  end,
})

return daojie
